//
//  TitleTextViewCell.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 1/15/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "TitleTextViewCell.h"

@implementation TitleTextViewCell

#pragma mark - Init
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        
        self.textFieldMode = TextFieldInputModeKeyboard;
        [self initializeTextView];
        
        [self.textField removeFromSuperview];
    }
    
    return self;
}

- (void)initializeTextView
{
    UITextView *textView = [[UITextView alloc] init];
    [textView setFont:FONT(FONT_SIZE_NORMAL)];
    [textView setBackgroundColor:[UIColor clearColor]];
    textView.delegate = self;
    textView.returnKeyType = UIReturnKeyDone;
    textView.inputAccessoryView = self.toolbar;
    [_subView addSubview:textView];
    
    _textView = textView;
}

#pragma mark - Logic functions
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    _textView.frame = RECT(self.titleLabel.right + PADDING, 0, _subView.width - self.titleLabel.right - PADDING, _subView.height);    
}
#pragma mark - Delegate
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    [self textFieldDidBeginEditing:nil];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self textFieldDidEndEditing:nil];
}

- (void)inputToolbarDidPressCloseButton:(InputToolbar *)toolbar
{
    if (self.textFieldMode == TextFieldInputModeTableView)
    {
        _ignoreRaiseDidSelectValue = YES;
        [_textView resignFirstResponder];
        _ignoreRaiseDidSelectValue = NO;
    }
    else
    {
        [_textView resignFirstResponder];
    }
}

- (void)inputToolbarDidPressRightButton:(InputToolbar *)toolbar
{
    [_textView resignFirstResponder];
}
@end
